package net.vkits.api;

import io.jsonwebtoken.Claims;
import io.swagger.annotations.ApiParam;
import net.vkits.admin.application.PostApplicationService;
import net.vkits.admin.domain.model.Post;
import net.vkits.admin.domain.model.SubPost;
import net.vkits.common.dto.BaseDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by ethan-liu on 2017/2/18.
 */
@Controller
public class PostsApiController implements PostsApi {
    private static final Logger logger = LoggerFactory.getLogger(PostsApiController.class);

    @Resource
    private HttpServletRequest request;
    @Resource
    private PostApplicationService postApplicationService;

    @Override
    public Page<Post> postsGetByParam(@ApiIgnore @PageableDefault(sort = {"id"}, direction = Sort.Direction.DESC)Pageable pageable) {
        final Claims claims = (Claims) request.getAttribute("claims");
        Page<Post> posts;
        if (null != claims) {
            logger.info("get claims {}", claims);
            String phone = claims.get("phone").toString();
            posts = postApplicationService.getPostsByCellphoneOrVisibilityTrue(phone, pageable);
        } else {
            logger.info("no claims");
            posts = postApplicationService.getPostsByVisibilityTrue(pageable);
        }
        return posts;
    }

    @Override
    public ResponseEntity<BaseDTO> addPost(@ApiParam(value = "发帖内容", required = true) @RequestBody Post post) {
        final Claims claims = (Claims) request.getAttribute("claims");
        String phone = claims.get("phone").toString();
        logger.info("Post is: {}", post);
        postApplicationService.addPost(post.getNickname(), phone, post.getAvatarUrl(),post.getContent(),0,false,0);
        BaseDTO baseDTO = new BaseDTO();
        baseDTO.setCode(200);
        baseDTO.setMsg("OK");
        return new ResponseEntity<>(baseDTO, HttpStatus.OK);
    }

    @Override
    public ResponseEntity<Post> getPost(@ApiParam(value = "主贴ID", required = true) @PathVariable Long id) {
        Post post = postApplicationService.getPost(id);
        return new ResponseEntity<>(post, HttpStatus.OK);
    }

    @Override
    public ResponseEntity<BaseDTO> addSubPost(@ApiParam(value = "评论内容", required = true) @RequestBody SubPost subPost) {
        logger.info("subPost is: {}", subPost);
        postApplicationService.addRealSubPost(subPost);
        BaseDTO baseDTO = new BaseDTO();
        baseDTO.setCode(200);
        baseDTO.setMsg("OK");
        return new ResponseEntity<>(baseDTO, HttpStatus.OK);
    }

    @Override
    public ResponseEntity<BaseDTO> likePost(@PathVariable Long id) {
        postApplicationService.likePost(id);
        BaseDTO baseDTO = new BaseDTO();
        baseDTO.setCode(200);
        baseDTO.setMsg("OK");
        return new ResponseEntity<>(baseDTO, HttpStatus.OK);
    }
}
